//
//  DownloadViewController.m
//  DMT_VR
//
//  Created by xin on 2016/10/26.
//  Copyright © 2016年 Elephant-Parade. All rights reserved.
//

#import "DownloadViewController.h"
#import "DMTMD360PlayViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <MBProgressHUD/MBProgressHUD.h>

#import "VedioModel.h"
#import "DownloadCell.h"
#import "HSDownloadManager.h"

#import "MJExtension.h"

@interface DownloadViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray              *     _downloadObjectArr;
    NSString                    *     _plistPath;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation DownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutUI];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)layoutUI{
    //消除tableview多余的分割线
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)initData{
    _downloadObjectArr = [NSMutableArray arrayWithArray:[VedioModel readDiskAllCache]];
    NSLog(@"_downloadObjectArr=%@",_downloadObjectArr);
    [self.tableView reloadData];
}

#pragma mark - IBAction

- (IBAction)backMethod:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)editMethod:(id)sender {
    
}

- (VedioModel *)vedioModelChangeIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [_downloadObjectArr[indexPath.row] allValues][0];
    return [VedioModel mj_objectWithKeyValues:dic];
}

#pragma mark - UITableViewDelegate UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [UIScreen mainScreen].bounds.size.width /1.5f;
    
    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _downloadObjectArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
    
    
    DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    //返回的数组是对象是字典格式为{url:vediomodel dic}
    
    VedioModel * model = [self vedioModelChangeIndexPath:indexPath];
    
    cell.model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    //删除数据
    VedioModel * model = [self vedioModelChangeIndexPath:indexPath];
    
    [[HSDownloadManager sharedInstance] deleteFile:model.vediourl];
    
    [VedioModel deleteCache:model];
    
    [_downloadObjectArr removeObjectAtIndex:row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VedioModel * model = [self vedioModelChangeIndexPath:indexPath];
    
    if(![[HSDownloadManager sharedInstance] isCompletion:model.vediourl])
    {
        return;
    }
    
    NSString * path = [[HSDownloadManager sharedInstance] completionFilePath:model.vediourl];
    NSFileManager * manager = [NSFileManager defaultManager];
    if([manager fileExistsAtPath:path]){
        NSURL * url = [NSURL fileURLWithPath:path];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DMTMD360PlayViewController" bundle:nil];
        DMTMD360PlayViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DMTMD360PlayViewController"];
        [self presentViewController:vc animated:YES completion:^{
            [vc initParams:url title:model.title_ch];
        }];
    }else
    {
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"文件走丢了，请删除该视频重新下载。";
        [hud hideAnimated:YES afterDelay:1.f];
    }
}

#pragma mark - 屏幕方向
- (BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

#pragma mark - 状态栏
//设置样式
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

//设置是否隐藏
- (BOOL)prefersStatusBarHidden {
    //    [super prefersStatusBarHidden];
    return NO;
}

//设置隐藏动画
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

@end
